package com.cadre.web.controller.officer;

import com.alibaba.fastjson.JSON;
import com.cadre.common.utils.R;
import com.cadre.common.utils.annotation.SysLog;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.common.utils.oldutil.ValidatorUtils;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.A91;
import com.cadre.system.domain.vo.ESUtil;
import com.cadre.system.service.A91Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@Api(value = "干部实绩", tags = {"干部实绩"})
@RequestMapping({"/a01/a91"})
@RestController
/* loaded from: A91Controller.class */
public class A91Controller {
    @Autowired
    private A91Service a91Service;
    @Autowired
    RestHighLevelClient client;

    @GetMapping({"/list/{personid}"})
    @ApiOperation("获取干部实绩列表")
    public R list(@PathVariable String personid) {
        return R.ok().put("data", (Object) this.a91Service.selectListByPersonid(personid));
    }

    @GetMapping({"/info/{flowid}"})
    @ApiOperation("获取一条干部实绩")
    public R info(@PathVariable("flowid") String flowid) {
        return R.ok().put("a91", (Object) ((A91) this.a91Service.getBaseMapper().selectById(flowid)));
    }

    @PostMapping({"/save"})
    @SysLog(value = "新增干部实绩信息", target = "A01", set = "A91", type = 0)
    @ApiOperation("添加干部实绩")
    public R save(@RequestBody A91 a91) throws IOException {
        a91.setFlowid(GenerateUUID.getUUID());
        this.a91Service.save(a91);
        Map<String, Object> source = this.client.get(new GetRequest(ESUtil.a01Index, a91.getPersonid()), RequestOptions.DEFAULT).getSource();
        if (source != null) {
            A01 a01s = ESUtil.useObjectMapper(source);
            a01s.setA91List(this.a91Service.selectListByPersonid(a91.getPersonid()));
            this.client.index(new IndexRequest(ESUtil.a01Index).id(a01s.getFlowid()).source(JSON.toJSONString(a01s), XContentType.JSON), RequestOptions.DEFAULT);
        }
        return R.ok();
    }

    @PostMapping({"/update"})
    @SysLog(value = "修改干部实绩信息", target = "A01", set = "A91", type = 1)
    @ApiOperation("干部实绩更新")
    public R update(@RequestBody A91 a91) throws IOException {
        ValidatorUtils.validateEntity(a91, new Class[0]);
        this.a91Service.getBaseMapper().updateById(a91);
        Map<String, Object> source = this.client.get(new GetRequest(ESUtil.a01Index, a91.getPersonid()), RequestOptions.DEFAULT).getSource();
        if (source != null) {
            A01 a01s = ESUtil.useObjectMapper(source);
            a01s.setA91List(this.a91Service.selectListByPersonid(a91.getPersonid()));
            this.client.index(new IndexRequest(ESUtil.a01Index).id(a01s.getFlowid()).source(JSON.toJSONString(a01s), XContentType.JSON), RequestOptions.DEFAULT);
        }
        return R.ok();
    }

    @PostMapping({"/delete"})
    @SysLog(value = "删除干部实绩信息", target = "A01", set = "A91", type = DateUtil.SUB_MONTH)
    @ApiOperation("干部实绩删除")
    public R delete(@RequestBody String[] flowids) {
        this.a91Service.getBaseMapper().deleteBatchIds(Arrays.asList(flowids));
        return R.ok();
    }
}
